package com.shop.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.shop.mapper.DtsPermissionMapper;
import com.shop.pojo.DtsPermission;
import com.shop.service.DtsPermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @ClassName DtsPermissionServiceImpl
 * @Author xiao宋
 * @Date 19:01  2022/10/13
 * @Version 1.0
 * @Description TODO
 */
@Service
public class DtsPermissionServiceImpl implements DtsPermissionService {
    @Autowired
    DtsPermissionMapper permissionMapper;
    @Override
    public Set<String> findPermsById(Integer[] roleIds) {
        LambdaQueryWrapper<DtsPermission>wrapper=new LambdaQueryWrapper<>();
//        数据库里存的是 1 0 分别对应的是 true 和 false  此处可以写数字或者 Boolean值
        wrapper.eq(DtsPermission::getDeleted, false);
//        权限 ID
        wrapper.in(DtsPermission::getRoleId,roleIds);
//        获取查询到的权限
        List<DtsPermission>permissionList= permissionMapper.selectList(wrapper);
        Set<String>perms=permissionList.stream().map(permission->{
            return permission.getPermission();
        }).collect(Collectors.toSet());
        return perms;
    }

    @Override
    public void updatePermissions(int roleId, List<String> permissions) {
//
        LambdaQueryWrapper<DtsPermission>w=new LambdaQueryWrapper<>();
        w.eq(DtsPermission::getRoleId, roleId);
        permissionMapper.delete(w);

        if (permissions!=null&&permissions.size()>0){
            for (String s : permissions) {
                DtsPermission p=new DtsPermission();
                p.setRoleId(roleId);
                p.setPermission(s);
                p.setAddTime(new Date());
                p.setUpdateTime(new Date());
                p.setDeleted(false);
                permissionMapper.insert(p);
            }
        }
    }
}
